const app = getApp()
import {
  parseTime
} from '../../../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    presentStageArr: [
      '待处理',
      '进行中',
      '已完成',
      '已逾期'
    ],
    orderStatus: [{
        label: '进行中',
        selected: false,
        value: 1
      },
      {
        label: '已完成',
        selected: false,
        value: 2
      },
      {
        label: '已逾期',
        selected: false,
        value: 3
      }
    ],
    isShow: true,
    orderData: [],
    pageData: {
      pageNo: 1,
      pageSize: 10
    },
    total:0,
    isLoading:false,
    queryFrom: {
      dateEnd: "",
      dateStart: "",
      nameOrPhone: "",
      responsibleId: '',
      status: []
    },
    isLock:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    app.globalData.search = ''
    if (options.type) {
      wx.setNavigationBarTitle({
        title: '自购服务订单',
      })
    }
    // this.getList()
  },
  getList() {
    let that = this
    that.setData({
      isLoading:true,
    })
    wx.showLoading({
      title: '加载中...',
      mask:true
    })
    wx.request({
      url: app.globalData.serverUrl + '/medical/order/page/' + that.data.pageData.pageNo + '/' + that.data.pageData.pageSize,
      method: 'POST',
      header: {
        'token': wx.getStorageSync('token')
      },
      data: that.data.queryFrom,
      success: (res => {
        let resp = res.data
        if (resp.code == 200) {
          resp.data.orderPage.records.map(item => {
            item.deviceReturnTime = parseTime(item.deviceReturnTime, '{m}月{d}日')
          })
          let arr = that.data.orderData.concat(resp.data.orderPage.records)
          that.setData({
            orderData: arr,
            total:resp.data.orderPage.total
          })
          wx.hideLoading()
        }
      }),
      complete(){
        that.setData({
          isLoading:false
        })
      }
    })
  },
  bindSearchChange(e) {
    this.setData({
      'queryFrom.dateEnd': e.detail.endDate == '结束日期' ? '' : e.detail.endDate,
      'queryFrom.dateStart': e.detail.startDate == '开始日期' ? '' : e.detail.startDate,
      'queryFrom.status': e.detail.statusArr,
      'queryFrom.responsibleId': e.detail.doctorsId,
      orderData:[],
      'pageData.pageNo':1
    })
    this.getList()
  },
  onShow() {
    this.setData({
      isLock:null
    })
    if (app.globalData.search == '') {
      this.setData({
        'queryFrom.nameOrPhone': '',
        'pageData.pageNo':1,
        orderData:[]
      })
      this.getList()
    }else{
      this.setData({
        'queryFrom.dateEnd': '',
        'queryFrom.dateStart': '',
        'queryFrom.status': [],
        'queryFrom.responsibleId': '',
        'queryFrom.nameOrPhone': app.globalData.search,
        'pageData.pageNo':1,
        orderData:[]

      })
      this.getList()
    }
  },
  /**
   * 用户点击跳转产品详情
   */
  toDetail(e) {
    // const status = e.currentTarget.dataset.status
    // const detailMap = {
    //   '进行中': '/pages/equipment/order/detail/going/going',
    //   '已逾期': '/pages/equipment/order/detail/overdue/overdue',
    //   '待处理': '/pages/equipment/order/detail/waiting/waiting',
    //   '已完成': '/pages/equipment/order/detail/completed/completed'
    // }
    // wx.navigateTo({
    //   url: detailMap[status],
    // })
    let that = this
    let lock=null
    if(!that.data.isLock){
      that.setData({
        isLock:new Date().getTime()
      })
    }else{
      lock = new Date().getTime()
    }
    if(that.data.isLock && lock){
      if(lock-that.data.isLock<app.globalData.isNumLock){
        return
      }else{
        that.setData({
          isLock:lock
        })
      }
    }
    wx.navigateTo({
      url: '/pagesA/orderswaiters/orderswaiters?id=' + e.currentTarget.dataset.id
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    // 1. 重置关键数据
    this.setData({
      'pageData.pageNo': 1,
      total: 0,
      isloading: false,
      orderData:[]
    })
    // 2. 重新发起请求 并关闭下拉窗口
    this.getList()
    wx.stopPullDownRefresh()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let that = this
    if(that.data.isLoading)return
    let pageNo =that.data.pageData.pageNo
    this.setData({
      'pageData.pageNo': pageNo += 1 // 让页码值自增 +1
    })
    if(that.data.orderData.length<that.data.total){
      that.getList() // 重新获取列表数据
    }else{
         wx.showToast({
           title: '暂无更多数据！',
           icon:'none'
         })
    }
  },
})